/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ConsultaFuncionario.java
 *
 * Created on 08/04/2013, 22:24:32
 */
package br.com.fatecpg.locacaobicicletas;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Davi
 */
public class ConsultaFuncionario extends javax.swing.JInternalFrame {

    /** Creates new form ConsultaFuncionario */
    public ConsultaFuncionario() {
        initComponents();
    }

    private void atualizarfunc() throws SQLException {
        DefaultTableModel model = (DefaultTableModel) funcionariojTable.getModel();
        model.setRowCount(0);
        String sql = "select *from funcionario";
        ResultSet rs = ConexãoBike.ConexãoJDBC.createStatement().executeQuery(sql);
        while (rs.next()) {
            SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");  
           
            model.addRow(new Object[]{
                
                        rs.getInt("cd_func"),
                        rs.getString("nm_func"),
                        rs.getLong("cd_cpf_func"),
                        sdf.format(rs.getDate("dt_nasc_func")),
                        rs.getLong("cd_tel_func"),
                        rs.getString("ic_gerente_sim_nao"),});
       }
    }
    private void Excluirfuncionario() throws SQLException {
        if (funcionariojTable.getSelectedRow() >= 0) {
            String item = funcionariojTable.getValueAt(funcionariojTable.getSelectedRow(), 0).toString();
            if (JOptionPane.showConfirmDialog(this, "Excluir Funcionario " + item) == JOptionPane.YES_NO_OPTION) {
                String sql = "delete from funcionario where cd_func =" + item;
                String sqlusu = "delete from usuario where cd_usuario =" + item;
                try {
                    ConexãoBike.ConexãoJDBC.createStatement().execute(sql);
                    ConexãoBike.ConexãoJDBC.createStatement().execute(sqlusu);
                    this.atualizarfunc();
                } catch (SQLException ex) {
                    JOptionPane.showMessageDialog(this, "Erro ao tentar excluir registro" + ex.getMessage(),
                            "Erro", JOptionPane.ERROR_MESSAGE);
                }
            }
        } else {
            JOptionPane.showMessageDialog(this, "Registro não Encontrado.",
                    "Aviso", JOptionPane.WARNING_MESSAGE);
        }
    }
    
     private void alterafuncionario() throws SQLException {
       
          DefaultTableModel modelo = (DefaultTableModel) funcionariojTable.getModel();
        int linhas = funcionariojTable.getRowCount();
        String SQL = "";
        
        for (int i = 0; i < linhas; i++) {
            int item = Integer.parseInt(funcionariojTable.getValueAt(i, 0).toString());
            String nmfunc = funcionariojTable.getValueAt(i, 1).toString().toUpperCase();
            String cpffunc = funcionariojTable.getValueAt(i, 2).toString();
            String nascfunc = funcionariojTable.getValueAt(i, 3).toString();
            String telfunc = funcionariojTable.getValueAt(i, 4).toString();
            String gerente = funcionariojTable.getValueAt(i, 5).toString().toUpperCase();
           
            SQL = "update funcionario " + "set nm_func = '" + nmfunc + "'"
            + ",cd_cpf_func = '"+ cpffunc +"'"
            + ",dt_nasc_func = '"+ nascfunc +"'"
            + ",cd_tel_func = "+ telfunc +""
            + ",ic_gerente_sim_nao = '"+ gerente +"'"
            + "where cd_func = " + item;
            try {
                ConexãoBike.ConexãoJDBC.createStatement().execute(SQL);
                //JOptionPane.showMessageDialog(null, "Alterações Efetuadas com Sucesso");
            } catch (SQLException ex) {
                JOptionPane.showMessageDialog(this, "Campos Não Alterados. Erro : "
                        + ex.getMessage(), "Erro", JOptionPane.ERROR_MESSAGE);
            }
        }
        JOptionPane.showMessageDialog(null, "Alterações Efetuadas com Sucesso");
      }   
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        funcionariojTable = new javax.swing.JTable();
        jPanel1 = new javax.swing.JPanel();
        consultarjButton = new javax.swing.JButton();
        excluirjButton = new javax.swing.JButton();
        alterarjButton = new javax.swing.JButton();
        cancelarrjButton = new javax.swing.JButton();

        setIconifiable(true);
        setMaximizable(true);
        setTitle("Consulta de Funcionarios");

        funcionariojTable.setAutoCreateRowSorter(true);
        funcionariojTable.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Código", "Nome", "CPF", "Data de Nascimento", "Telefone", "Gerênte"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.Integer.class, java.lang.String.class, java.lang.Long.class, java.lang.String.class, java.lang.Long.class, java.lang.String.class
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }
        });
        jScrollPane1.setViewportView(funcionariojTable);

        jPanel1.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
        jPanel1.setLayout(new java.awt.GridLayout(1, 0));

        consultarjButton.setFont(new java.awt.Font("Tahoma", 0, 14));
        consultarjButton.setText("CONSULTAR");
        consultarjButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                consultarjButtonActionPerformed(evt);
            }
        });
        jPanel1.add(consultarjButton);

        excluirjButton.setFont(new java.awt.Font("Tahoma", 0, 14));
        excluirjButton.setText("EXCLUIR");
        excluirjButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                excluirjButtonActionPerformed(evt);
            }
        });
        jPanel1.add(excluirjButton);

        alterarjButton.setFont(new java.awt.Font("Tahoma", 0, 14));
        alterarjButton.setText("ALTERAR");
        alterarjButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                alterarjButtonActionPerformed(evt);
            }
        });
        jPanel1.add(alterarjButton);

        cancelarrjButton.setFont(new java.awt.Font("Tahoma", 0, 14));
        cancelarrjButton.setText("CANCELAR");
        cancelarrjButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelarrjButtonActionPerformed(evt);
            }
        });
        jPanel1.add(cancelarrjButton);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 513, Short.MAX_VALUE)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, 513, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap(74, Short.MAX_VALUE)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 317, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 43, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addContainerGap(348, Short.MAX_VALUE)))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

private void consultarjButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_consultarjButtonActionPerformed
 try {
        atualizarfunc();
    } catch (SQLException ex) {
        JOptionPane.showMessageDialog(this, "Erro:" + ex.getMessage(), "erro", JOptionPane.ERROR_MESSAGE);
    }
 // TODO add your handling code here:
}//GEN-LAST:event_consultarjButtonActionPerformed

private void excluirjButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_excluirjButtonActionPerformed
 try {
        Excluirfuncionario();
    } catch (SQLException ex) {
        JOptionPane.showMessageDialog(this, "Erro:" + ex.getMessage(), "erro", JOptionPane.ERROR_MESSAGE);
    }

    
    // TODO add your handling code here:
}//GEN-LAST:event_excluirjButtonActionPerformed

private void alterarjButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_alterarjButtonActionPerformed
 try {
 alterafuncionario(); 
 }catch(SQLException ex){
 JOptionPane.showMessageDialog(this, "Erro:" + ex.getMessage(), "erro", JOptionPane.ERROR_MESSAGE);
 }
        // TODO add your handling code here:
}//GEN-LAST:event_alterarjButtonActionPerformed

private void cancelarrjButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelarrjButtonActionPerformed
this.setVisible(false);
    // TODO add your handling code here:
}//GEN-LAST:event_cancelarrjButtonActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton alterarjButton;
    private javax.swing.JButton cancelarrjButton;
    private javax.swing.JButton consultarjButton;
    private javax.swing.JButton excluirjButton;
    private javax.swing.JTable funcionariojTable;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    // End of variables declaration//GEN-END:variables
}
